package com.jzr.board.api

import com.google.gson.Gson
import com.jzr.board.base.Constant
import com.jzr.board.bean.*
import com.jzr.board.bean.body.RecordBody
import com.jzr.board.bean.body.UpdateCareLabelBody
import com.jzr.board.bean.body.UpdateNursingBody
import com.jzr.board.utils.CommonUtil
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.QueryMap
import rx.Observable
import java.util.*


class Api(okHttpClient: OkHttpClient) {

    init {
        val retrofit: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(okHttpClient)
            .baseUrl(CommonUtil.getBaseUrl())
            .build()
        service =
            retrofit.create(ApiService::class.java)
    }

    companion object {
        private var instance: Api? = null
        private var service: ApiService? = null
        fun getInstance(): Api? {
            synchronized(Api::class.java) {
                if (instance == null) {
                    instance =
                        Okhttp.provideOkHttpClient()?.let { Api(it) }
                }
                return instance
            }
        }

        fun getMap(
            vararg s: String
        ): Map<String, String>? {
            val map: MutableMap<String, String> =
                HashMap()
            if (s.size % 2 != 0) {
                return null
            } else {
                var i = 0
                while (i < s.size) {
                    map[s[i]] = s[++i]
                    i++
                }
            }
            return map
        }
    }


    //连接测试
    fun connectTest(): rx.Observable<ConnectTestBean?>? {
        return service?.connectTest()
    }


    //选择科室
    fun getCheckDeptNo(): rx.Observable<DeptCodeListBean?>? {
        return service?.getCheckDeptNo()
    }


    //绑定床位号
    fun getBindingDeptNo( s: RecordBody?): rx.Observable<BindingDeptBean?>? {
        return service?.getBindingDeptNo(s!!)
    }

    // 获取设备信息
    fun getDeviceInfo(vararg s: String?): rx.Observable<DeviceInfoBean?>? {
        return service?.getDeviceInfo(getMap(*s as Array<out String>)!!)
    }

    // 获取病人列表
    fun getPatientList(vararg s: String?): rx.Observable<PatientListBean?>? {
        return service?.getPatientList(getMap(*s as Array<out String>)!!)
    }

    // 获取今天排班
    fun getTodayPlan(vararg s: String?): rx.Observable<NurseSchedulBean?>? {
        return service?.getTodayPlan(getMap(*s as Array<out String>)!!)
    }

    // 获取今日汇总
    fun getBoardGet(vararg s: String?): rx.Observable<BoardGetBean?>? {
        return service?.getBoardGet(getMap(*s as Array<out String>)!!)
    }

    // 根据deptCoe获取护士词典列表
    fun getNurseCodeList(vararg s: String?): rx.Observable<NurseListBean?>? {
        return service?.getNurseCodeList(getMap(*s as Array<out String>)!!)
    }

    // 获取标签分类
    fun getCategoryList(vararg s: String?): rx.Observable<CategoryListBean?>? {
        return service?.getCategoryList(getMap(*s as Array<out String>)!!)
    }

    // 根据标签分类获取词典列表
    fun getCareLabelList(vararg s: String?): rx.Observable<CareLabelListBean?>? {
        return service?.getCareLabelList(getMap(*s as Array<out String>)!!)
    }

    // 更新护理标签
    fun getUpdateCareLabel( record: UpdateCareLabelBody?): rx.Observable<BaseBean?>? {
        return service?.getUpdateCareLabel(record!!)
    }

    // 获取护理标签列表
    fun getPatientCareLabelList( vararg s: String?): rx.Observable<PatientCareLabelListBean?>? {
        return service?.getPatientCareLabelList(getMap(*s as Array<out String>)!!)
    }

    // 更新护理信息
    fun getUpdateNursing( body: UpdateNursingBody): rx.Observable<BaseBean?>? {
        return service?.getUpdateNursing(body)
    }

    // 获取最新版本
    fun getLastVersion(vararg s: String?): rx.Observable<LastVersionBean?>? {
        return service?.getLastVersion(getMap(*s as Array<out String>)!!)
    }

    // 获取费用明细列表
    fun getFeeRecordList(vararg s: String?): rx.Observable<FeeRecordListBean?>? {
        return service?.getFeeRecordList(getMap(*s as Array<out String>)!!)
    }

    // 获取检查报告列表
    fun getTestReportList(vararg s: String?): rx.Observable<TestReportListBean?>? {
        return service?.getTestReportList(getMap(*s as Array<out String>)!!)
    }

    // 获取检验报告列表
    fun getInspectReportList(vararg s: String?): rx.Observable<InspectReportListBean?>? {
        return service?.getInspectReportList(getMap(*s as Array<out String>)!!)
    }

    // 获取医嘱列表
    fun getOrderList(vararg s: String?): rx.Observable<OrderListBean?>? {
        return service?.getOrderList(getMap(*s as Array<out String>)!!)
    }

    // 获取体温列表
    fun getTemperatureList(vararg s: String?): rx.Observable<TemBean?>? {
        return service?.getTemperatureList(getMap(*s as Array<out String>)!!)
    }

    // 获取血压列表
    fun getBloodPressList(vararg s: String?): rx.Observable<BloodBean?>? {
        return service?.getBloodPressList(getMap(*s as Array<out String>)!!)
    }

    // 获取血压列表
    fun getOxymeterList(vararg s: String?): rx.Observable<BloodBean?>? {
        return service?.getOxymeterList(getMap(*s as Array<out String>)!!)
    }


}